﻿namespace Foundation.Core
{
    public class SelectItemOptions : SelectExtend
    {
        public SelectItemOptions(string key, SelectCategory category)
        {
            Key = key;
            Category = category;
        }

        public string Key { get; }

        public SelectCategory Category { get; }

        public Type ItemDeclaringType { get; set; }

        public SelectItemLoadMethodOptions MethodOptions { get; protected set; }

        public IList<ISelectItem> Items { get; set; }

        /// <summary>
        /// 缓存时长
        /// </summary>
        public TimeSpan? CacheTime { get; set; }

        public SemaphoreSlim Slim { get; } = new SemaphoreSlim(1, 1);

        public Type Factory { get; protected set; }

        public Type CreatorType { get; protected set; }

        public ISelectCollectionCreator Creator { get; protected set; }

        public SelectItemOptions AddFactory<T>()
            where T : class, ISelectCollectionCreatorFactory
        {
            Factory = typeof(T);

            return this;
        }

        protected internal SelectItemOptions AddFactory(Type type)
        {
            if (typeof(ISelectCollectionCreatorFactory).IsAssignableFrom(type))
            {
                Factory = type;
            }

            return this;
        }

        public SelectItemOptions AddCreatorType<T>()
            where T : class, ISelectCollectionCreator
        {
            CreatorType = typeof(T);

            return this;
        }

        protected internal SelectItemOptions AddCreatorType(Type type)
        {
            if (typeof(ISelectCollectionCreator).IsAssignableFrom(type))
            {
                CreatorType = type;
            }

            return this;
        }

        public SelectItemOptions AddCreator(ISelectCollectionCreator creator)
        {
            Creator = creator;

            return this;
        }

        public void SetMehtodOptions(SelectItemLoadMethodOptions options)
        {
            this.MethodOptions = options;
        }
    }
}
